package com.zklh.leetcode.other.problem28;
/**
 * @descrpiton 实现strStr()
 * @author 坐看落花
 * @version 1.0
 * @date 2019/10/18
 */
public class Solution {
    public int strStr(String haystack, String needle) {
        if(needle == null || needle.length() == 0){
            return 0;
        }
        if(haystack == null || haystack.length() < needle.length()){
            return -1;
        }
        // 表示指向needleChars的指针
        int n = 0;
        int result = -1;
        for (int h = 0; h < haystack.length(); h++) {
            n = 0;
            while(h+n < haystack.length() && haystack.charAt(h+n) == needle.charAt(n) && ++n < needle.length());
            if(n == needle.length()){
                result = h;
                break;
            }
        }
        return result;
    }

    public static void main(String[] args){
        Solution solution = new Solution();
        System.out.println(solution.strStr("aaaa", "bba"));
    }

}

